home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagn_r.zip / NUMBERS.SWG / 0005_Rotate Bits LEFT-RIGHT.pas < prev    next >
Pascal/Delphi Source File  |  1993-05-28  |  2KB  |  52 lines

  1. > I made a Program in Turbo-Pascal that rotates the bits in one Byte so I can
  2. > encrypt/decrypt a File, however the routine is slow. I then made the same
  3. > Program in turbo-C using _RotLeft and _RotRight, the speed of execution was
  4. > Really faster than Turbo-Pascal. Does anybody know of a way to rotate the
  5. > bits of one Byte in turbo-Pascal and FAST !!!!
  6.  
  7.  
  8.         Since 80xxx CPUs have bit rotate instructions (ROL, ROR), it would
  9. be a shame to use some clumsy HLL Construct. BTW, I'm sure _RotLeft and
  10. _RotRight use rotate instructions too, possibly insert them Inline. If
  11. you are using TP 6.0+, try something like this:
  12.  
  13. { to rotate left }
  14. Function RotLeft(B, Count: Byte): Byte; Assembler;
  15. Asm
  16.   mov   al, B
  17.   mov   cl, Count
  18.   rol   al, cl
  19. end;
  20.  
  21. { to rotate right }
  22. Function RotRight(B, Count: Byte): Byte; Assembler;
  23. Asm
  24.   mov   al, B
  25.   mov   cl, Count
  26.   ror   al, cl
  27. end;
  28.  
  29.  
  30.         Of course, if you need to do this in only a few places it would
  31. be better not to define Functions, but insert Asm blocks in your code
  32. directly.
  33.  
  34.         The fastest Pascal way to rotate Byte would be something like
  35. this:
  36.  
  37. Function RotLeft(B, Count: Byte): Byte;
  38. Var
  39.   W : Word;
  40.   A : Array[0..1] of Byte Absolute W;
  41. begin
  42.   A[0] := B;
  43.   A[1] := B;
  44.   W := W shl Count;
  45.   RotLeft := A[1];
  46. end;
  47.  
  48.         To rotate right With this method, you would shift right and
  49. return A[0]. I would like to think this is as fast as it gets in TP
  50. without assembly, but one can never be sure <g>. Anyway, I recommend
  51. the assembly solution over this one, it is faster and more elegant.
  52.